﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Xml;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class CommonService : System.Web.Services.WebService
{
    public static string Conn = "Database='test1610';Data Source='localhost';User Id='root';Password='admin';charset='utf8';pooling=true";
    private MySqlConnection conn;
    public CommonService() {
        conn = new  MySqlConnection(Conn);
        conn.Open();    //建立连接，打开数据库
        Console.WriteLine("connect database is success");
        //如果使用设计的组件，请取消注释以下行
        //InitializeComponent(); 
    }

    [WebMethod (Description = "test")]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod (Description = "检查用户登录信息，返回用户角色类")]
    public string CheckUserLogin(string username,string password)
    {
        // string sql =  select * from tmember where UserName = 'admin' and M_Password = '111111';
        string sql = @"select * from tmember where UserName = @myUsername and M_Password = @myPassword";
        MySqlCommand checkUser = new MySqlCommand(sql, conn);
        checkUser.Parameters.AddWithValue("@myUsername", username);
        checkUser.Parameters.AddWithValue("@myPassword", password);
        MySqlDataReader reader = checkUser.ExecuteReader();
        if (reader.Read() != false)   //遍历表中数据
        {
            //读取并打印数据
            reader.Read();
            string memberRole = reader[4].ToString();
            checkUser.Dispose();
            return memberRole;
        }
        checkUser.Dispose();
        return "-1";
    }


    [WebMethod (Description = "返回指定用户相关的手术列表")]
    public XmlDocument GetOperationListByUsername(string myOperator)
    {
        XmlDocument xml = new XmlDocument();
        XmlElement rootNode = xml.CreateElement(myOperator);
        xml.AppendChild(rootNode);

        string sql = @"select OperationID,RoomID,OperationName,O_StartTime,O_EndTime from toperation where Operator = @myOperator";
        MySqlCommand checkOperator = new MySqlCommand(sql, conn);
        checkOperator.Parameters.AddWithValue("@myOperator", myOperator);
        MySqlDataReader reader = checkOperator.ExecuteReader();

        string str = "";
        while(reader.Read())
        {
            string operation_id = reader[0].ToString();
            string room_id = reader[1].ToString();
            string operation_name = reader[2].ToString();
            string o_startTime = reader[3].ToString();
            string o_endTime = reader[4].ToString();

            XmlElement child = xml.CreateElement("operator");
            child.SetAttribute("name", "operator");
            rootNode.AppendChild(child);

            XmlElement child_1 = xml.CreateElement("OperationID");
            XmlText OperationID_text = xml.CreateTextNode(operation_id);
            child_1.AppendChild(OperationID_text);
            child.AppendChild(child_1);

            XmlElement child_2 = xml.CreateElement("RoomID");
            XmlText RoomID_text = xml.CreateTextNode(room_id);
            child_2.AppendChild(RoomID_text);
            child.AppendChild(child_2);

            XmlElement child_3 = xml.CreateElement("OperationName");
            XmlText OperationName_text = xml.CreateTextNode(operation_name);
            child_3.AppendChild(OperationName_text);
            child.AppendChild(child_3);

            XmlElement child_4 = xml.CreateElement("O_StartTime");
            XmlText O_StartTime_text = xml.CreateTextNode(o_startTime);
            child_4.AppendChild(O_StartTime_text);
            child.AppendChild(child_4);

            XmlElement child_5 = xml.CreateElement("O_EndTime");
            XmlText O_EndTime_text = xml.CreateTextNode(o_endTime);
            child_5.AppendChild(O_EndTime_text);
            child.AppendChild(child_5);

            str += operation_id + " " + room_id + " " + operation_name + " " + o_startTime + " " + o_endTime + "\n" ;
        }

        checkOperator.Dispose();
        return xml;
    }

    [WebMethod (Description = "返回开放的手术列表（已结束的不返回）")]
    public XmlDocument GetPublicOperationList()
    {
        XmlDocument xml = new XmlDocument();
        XmlElement rootNode = xml.CreateElement("OperationList");
        xml.AppendChild(rootNode);

        string sql = "select RoomID,RecorderID,UserName,RoomName,DefaultVideoSourceID,VideoSourceCount,RoomType from troom";
        MySqlCommand checkOperationList = new MySqlCommand(sql, conn);
        MySqlDataReader reader = checkOperationList.ExecuteReader();

        while (reader.Read())
        {
            string room_id = reader[0].ToString();
            string recorder_id = reader[1].ToString();
            string username = reader[2].ToString();
            string roomname = reader[3].ToString();
            string defaultVideo_id = reader[4].ToString();
            string sourceCount = reader[5].ToString();
            string roomType = reader[6].ToString();

            if(roomType == "1")
            {
                XmlElement child = xml.CreateElement("operatorRoom");
                child.SetAttribute("roomname", roomname);
                rootNode.AppendChild(child);

                XmlElement child_1 = xml.CreateElement("RoomID");
                XmlText RoomID_text = xml.CreateTextNode(room_id);
                child_1.AppendChild(RoomID_text);
                child.AppendChild(child_1);

                XmlElement child_2 = xml.CreateElement("RecorderID");
                XmlText RecorderID_text = xml.CreateTextNode(recorder_id);
                child_2.AppendChild(RecorderID_text);
                child.AppendChild(child_2);

                XmlElement child_3 = xml.CreateElement("UserName");
                XmlText UserName_text = xml.CreateTextNode(username);
                child_3.AppendChild(UserName_text);
                child.AppendChild(child_3);

                XmlElement child_4 = xml.CreateElement("RoomName");
                XmlText RoomName_text = xml.CreateTextNode(roomname);
                child_4.AppendChild(RoomName_text);
                child.AppendChild(child_4);

                XmlElement child_5 = xml.CreateElement("DefaultVideoSourceID");
                XmlText DefaultVideoSourceID_text = xml.CreateTextNode(defaultVideo_id);
                child_5.AppendChild(DefaultVideoSourceID_text);
                child.AppendChild(child_5);

                XmlElement child_6 = xml.CreateElement("VideoSourceCount");
                XmlText VideoSourceCount_text = xml.CreateTextNode(sourceCount);
                child_6.AppendChild(VideoSourceCount_text);
                child.AppendChild(child_6);

                XmlElement child_7 = xml.CreateElement("RoomType");
                XmlText RoomType_text = xml.CreateTextNode(roomType);
                child_7.AppendChild(RoomType_text);
                child.AppendChild(child_7);
            }

        }

        checkOperationList.Dispose();
        return xml;
    }


    [WebMethod(Description = "返回开放的有录制的手术列表")]
    public XmlDocument GetPublicRecordingList()
    {
        XmlDocument xml = new XmlDocument();
        XmlElement rootNode = xml.CreateElement("RecordingList");
        xml.AppendChild(rootNode);

        string sql = @"select RecordingID,RecorderID,VideoID,OperationID,RecordingName,RecordingFile,RecordingTime,R_StartTime,R_EndTime,R_GroupID,CanPlayback,HighBits from trecording";
        MySqlCommand checkOperator = new MySqlCommand(sql, conn);
        MySqlDataReader reader = checkOperator.ExecuteReader();

        while (reader.Read())
        {
            XmlElement child = xml.CreateElement("RecordingList");
            child.SetAttribute("name", "RecordingList");
            rootNode.AppendChild(child);

            XmlElement child_1 = xml.CreateElement("RecordingID");
            XmlText RecordingID_text = xml.CreateTextNode(reader[0].ToString());
            child_1.AppendChild(RecordingID_text);
            child.AppendChild(child_1);

            XmlElement child_2 = xml.CreateElement("RecorderID");
            XmlText RecorderID_text = xml.CreateTextNode(reader[1].ToString());
            child_2.AppendChild(RecorderID_text);
            child.AppendChild(child_2);

            XmlElement child_3 = xml.CreateElement("VideoID");
            XmlText VideoID_text = xml.CreateTextNode(reader[2].ToString());
            child_3.AppendChild(VideoID_text);
            child.AppendChild(child_3);

            XmlElement child_4 = xml.CreateElement("OperationID");
            XmlText OperationID_text = xml.CreateTextNode(reader[3].ToString());
            child_4.AppendChild(OperationID_text);
            child.AppendChild(child_4);

            XmlElement child_5 = xml.CreateElement("RecordingName");
            XmlText RecordingName_text = xml.CreateTextNode(reader[4].ToString());
            child_5.AppendChild(RecordingName_text);
            child.AppendChild(child_5);

            XmlElement child_6 = xml.CreateElement("RecordingFile");
            XmlText RecordingFile_text = xml.CreateTextNode(reader[5].ToString());
            child_6.AppendChild(RecordingFile_text);
            child.AppendChild(child_6);

            XmlElement child_7 = xml.CreateElement("RecordingTime");
            XmlText RecordingTime_text = xml.CreateTextNode(reader[6].ToString());
            child_7.AppendChild(RecordingTime_text);
            child.AppendChild(child_7);

            XmlElement child_8 = xml.CreateElement("R_StartTime");
            XmlText R_StartTime_text = xml.CreateTextNode(reader[7].ToString());
            child_8.AppendChild(R_StartTime_text);
            child.AppendChild(child_8);

            XmlElement child_9 = xml.CreateElement("R_EndTime");
            XmlText R_EndTime_text = xml.CreateTextNode(reader[8].ToString());
            child_9.AppendChild(R_EndTime_text);
            child.AppendChild(child_9);

            XmlElement child_10 = xml.CreateElement("R_GroupID");
            XmlText R_GroupID_text = xml.CreateTextNode(reader[9].ToString());
            child_10.AppendChild(R_GroupID_text);
            child.AppendChild(child_10);

            XmlElement child_11 = xml.CreateElement("CanPlayback");
            XmlText CanPlayback_text = xml.CreateTextNode(reader[10].ToString());
            child_11.AppendChild(CanPlayback_text);
            child.AppendChild(child_11);

            XmlElement child_12 = xml.CreateElement("HighBits");
            XmlText HighBits_text = xml.CreateTextNode(reader[11].ToString());
            child_12.AppendChild(HighBits_text);
            child.AppendChild(child_12);
        }

        checkOperator.Dispose();
        return xml;
    }
}